package org.tinaelle.client.utils;

import org.tinaelle.exceptions.UserVisibleException;

public final class ExceptionTool {
	private ExceptionTool() {

	}

	public static String extractUserMessage(final Throwable tException) {
		Throwable tExceptionCopy = tException;
		while ( ( tExceptionCopy != null ) && !( tExceptionCopy instanceof UserVisibleException ) ) {
			tExceptionCopy = tExceptionCopy.getCause();
		}
		if ( tExceptionCopy == null ) {
			tExceptionCopy = tException;
		}
		return tExceptionCopy.getMessage();
	}

	public static String fullDetails(final Throwable tException) {
		Throwable tExceptionCopy = tException;
		final StringBuffer fullDetailString = new StringBuffer();
		while ( tExceptionCopy != null ) {
			fullDetailString.append( tExceptionCopy + ":" + tExceptionCopy.getMessage() + "\n\n" );
			tExceptionCopy = tExceptionCopy.getCause();
		}
		return fullDetailString.toString();
	}
}
